﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Pdm.Commons;
using Test;
using Pdm.Solver;

namespace Pdm.Wpf
{
    /// <summary>
    /// Lädt alle notwendigen Daten in GUI kompatiblen Datenformaten,
    /// Schnittstelle zum BLL(Solver, etc.), MVVM Pattern ViewModel
    /// </summary>
    /// 
    public class DataProvider
    {
        public static SortedList<double, List<PdmOptimization>> loadOptimizations()
        {
            //identify all alpha transitions for all dmus -> bisection
            //calculate PdmOptimizations for all alpha steps to be shown
            //return sorted List with alpha -> and sorted List of PdmOptimizations

            SortedList<double, List<PdmOptimization>> displayList = new SortedList<double, List<PdmOptimization>>();
            DmuList dmus = Program.getSampleList();
            //double[] urmWeights = new double[] { 1, 21, 34, 11, 13, 15, 22, 32, 21 };

            List<SpecificationInstance> urmWeights = new List<SpecificationInstance>();
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[0], 1));
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[0], 11));
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[1], 21));
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[2], 34));
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[3], 11));
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[4], 13));
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[5], 15));
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[6], 22));
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[7], 32));
            urmWeights.Add(new SpecificationInstance(dmus.coefficients[8], 21));

            foreach (Dmu dmu in dmus)
            {
                Bisection b = new Bisection(dmus, dmu, urmWeights, Bisection.SolverModell.Heuristik, null);
                List<PdmOptimization> trans = b.getTransitions();
                foreach (PdmOptimization opt in trans)
                {
                    if (!displayList.ContainsKey(opt.alpha))
                    {
                        displayList[opt.alpha] = new List<PdmOptimization>();
                    }
                    displayList[opt.alpha].Add(opt);
                }
            }
            HeuristikSolver sol;
            foreach (double alpha in displayList.Keys)
            {

                foreach (Dmu dmu in dmus)
                {
                    bool exists = false;
                    foreach (PdmOptimization opt in displayList[alpha])
                    {
                        if (opt.objectiveDmu.id == dmu.id)
                        {
                            exists = true;
                        }
                    }
                    if (!exists)
                    {
                        sol = new HeuristikSolver(dmus, dmu, urmWeights);
                        displayList[alpha].Add(sol.getOptimization(alpha));
                    }
                }
                displayList[alpha].Sort(new Comparison<PdmOptimization>(comparePdmOptimizationsByDmuId));
            }

            return displayList;
        }
        private static int comparePdmOptimizationsByDmuId(PdmOptimization left, PdmOptimization right)
        {
            if (left.objectiveDmu.id > right.objectiveDmu.id)
            {
                return 1;
            }
            else if (left.objectiveDmu.id < right.objectiveDmu.id)
            {
                return -1;
            }
            return 0;
        }
    }
}
